﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Marmat.Forms
{
    internal partial class BandCollectionEditorForm : Form
    {
        public BandCollectionEditorForm(BaseGrid Grid, Views.Grid.BandCollection Collection)
        {

            // 此调用是设计器所必需的。
            this.InitializeComponent();

            // 在 InitializeComponent() 调用之后添加任何初始化。
            this.Collection = Collection;
            foreach (Views.Grid.Column Column in Grid.Columns)
            {
                var GridColumn = this.BandedGridView1.Columns.Add();
                GridColumn.Name = Column.Name;
                GridColumn.Caption = Column.Caption;
                GridColumn.Tag = Column;
                this.Dictionary2.Add(Column, GridColumn);
            }

            this.CreateView(Collection, this.BandedGridView1.Bands);
        }

        private Views.Grid.BandCollection Collection;
        private Dictionary<Views.Grid.Column, DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn> Dictionary2 = new Dictionary<Views.Grid.Column, DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn>();

        public void CreateView(Views.Grid.BandCollection Collection, DevExpress.XtraGrid.Views.BandedGrid.GridBandCollection Bands)
        {
            foreach (Views.Grid.Band item in Collection)
            {
                if (Bands.Contains(item._band) == false)
                {
                    var GridBand = Bands.Add();
                    GridBand.Tag = item;
                    GridBand.Name = item.Name;
                    GridBand.Caption = item.Caption;
                    this.Dictionary.Add(item, GridBand);
                    foreach (Views.Grid.Column Column in item.Columns)
                    {
                        if (this.Dictionary2.ContainsKey(Column))
                        {
                            var GridColumn = this.Dictionary2[Column];
                            GridColumn.Visible = true;
                            GridBand.Columns.Add(GridColumn);
                        }
                    }

                    if (item.Bands.Count > 0)
                    {
                        this.CreateView(item.Bands, GridBand.Children);
                    }
                }
            }
        }

        private void BButton1_Click(object sender, EventArgs e)
        {
            var Band = new Views.Grid.Band();
            int index = 1;
            do
            {
                string Name = "Band" + index;
                if (this.Collection.Contains(Name) == false)
                {
                    Band.Name = Name;
                    break;
                }

                index += 1;
            }
            while (true);
            this.Collection.Add(Band);
            var GridBand = this.BandedGridView1.Bands.Add();
            GridBand.Tag = Band;
            GridBand.Name = Band.Name;
            GridBand.Caption = Band.Caption;
            this.Dictionary.Add(Band, GridBand);
            this.BPropertyGrid1.SelectedObject = Band;
        }

        private Dictionary<Views.Grid.Band, DevExpress.XtraGrid.Views.BandedGrid.GridBand> Dictionary = new Dictionary<Views.Grid.Band, DevExpress.XtraGrid.Views.BandedGrid.GridBand>();

        private void GridControl1_MouseDown(object sender, MouseEventArgs e)
        {
            var Hit = this.BandedGridView1.CalcHitInfo(e.Location);
            if (Hit.InBandPanel)
            {
                var gbl = Hit.Band;
                if (gbl is object)
                {
                    Views.Grid.Band Band = (Views.Grid.Band)gbl.Tag;
                    this.BPropertyGrid1.SelectedObject = Band;
                }
            }
        }

        private void BButton2_Click(object sender, EventArgs e)
        {
            if (this._BButton2.Text == "显示")
            {
                this.BandedGridView1.ShowCustomization();
            }
            else
            {
                this.BandedGridView1.HideCustomization();
            }
        }

        private void BButton3_Click(object sender, EventArgs e)
        {
            if (this.BPropertyGrid1.SelectedObject is object)
            {
                Views.Grid.Band Band = (Views.Grid.Band)this.BPropertyGrid1.SelectedObject;
                if (Band.Parent is null)
                {
                    this.Collection.Remove(Band);
                }
                else
                {
                    Band.Parent.Bands.Remove(Band);
                }

                this.Dictionary[Band].Dispose();
                this.BPropertyGrid1.SelectedObject = null;
            }
        }

        private void BButton4_Click(object sender, EventArgs e)
        {
            this.BandedGridView1.Bands.Clear();
        }

        private void BandedGridView1_ShowCustomizationForm(object sender, EventArgs e)
        {
            this._BButton2.Text = "隐藏";
        }

        private void BandedGridView1_HideCustomizationForm(object sender, EventArgs e)
        {
            this._BButton2.Text = "显示";
        }
    }
}